package com.prevpn.api;

import cn.hutool.core.lang.TypeReference;
import cn.hutool.json.JSONUtil;
import com.prevpn.common.CommonResponse;
import com.prevpn.common.CommonUtil;
import org.springframework.stereotype.Component;

import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

/**
 * OpenAPI - Web 应用相关接口
 */
@Component
public class AppApi {

    /**
     * 调用 aTrust OpenAPI 创建 Web 应用
     */
    public CommonResponse<Map<String, Object>> createWebApp(String jsonBody) {
        String path = "/api/v1/resource/createResource";
        String response = CommonUtil.openApiPostRequest(path, jsonBody);

        return JSONUtil.toBean(
                response,
                new TypeReference<CommonResponse<Map<String, Object>>>() {},
                false
        );
    }

    /**
     * 创建隧道应用（L3VPN/TUN2WEB）
     */
    public CommonResponse<Map<String, Object>> createTunnelApp(String jsonBody) {
        String path = "/api/v1/resource/createResource";
        String response = CommonUtil.openApiPostRequest(path, jsonBody);
        return JSONUtil.toBean(response,
                new TypeReference<CommonResponse<Map<String, Object>>>() {}, false);
    }

    /**
     * 基于应用名称授权（角色、用户或组织架构）
     */
    //public CommonResponse<Map<String, Object>> assignByName(String jsonBody) {
    //    String path = "/api/v3/resource/assignByName";
    //    String response = CommonUtil.openApiPostRequest(path, jsonBody);
    //
    //    return JSONUtil.toBean(response,
    //            new TypeReference<CommonResponse<Map<String, Object>>>() {}, false);
    //}

    private static final String QUERY_BY_NAME_PATH = "/api/v3/resource/queryByName";

    /**
     * 基于应用名称查询应用详情
     * @param name 应用名称
     * @return CommonResponse<Map<String,Object>>
     */
    public CommonResponse<Map<String, Object>> queryAppByName(String name) {
        // ✅ 定义接口路径
        String path = QUERY_BY_NAME_PATH;

        // ✅ 组装 GET 请求参数
        Map<String, Object> params = new HashMap<>();
        params.put("name", name);

        // ✅ 调用 OpenAPI
        String response = CommonUtil.openApiGetRequest(path, params);

        // ✅ 转换 JSON 响应
        return JSONUtil.toBean(
                response,
                new TypeReference<CommonResponse<Map<String, Object>>>() {},
                false
        );
    }

    /**
     * 查询应用列表
     * @param params 查询参数
     * @return CommonResponse<Map<String,Object>>
     */
    public CommonResponse<Map<String, Object>> queryAppList(Map<String, Object> params) {
        String path = "/api/v3/resource/queryAll";
        String response = CommonUtil.openApiGetRequest(path, params);
        return JSONUtil.toBean(response, new TypeReference<CommonResponse<Map<String, Object>>>() {}, false);
    }

    private static final String DELETE_RESOURCE_PATH = "/api/v1/resource/deleteResource";

    /**
     * 批量删除应用（基于名称或ID）
     */
    public CommonResponse<Map<String, Object>> deleteAppResource(String jsonBody) {
        String response = CommonUtil.openApiPostRequest(DELETE_RESOURCE_PATH, jsonBody);
        return JSONUtil.toBean(response, new TypeReference<CommonResponse<Map<String, Object>>>() {}, false);
    }

    private static final String UPDATE_WEB_APP_PATH = "/api/v1/resource/updateResource";

    /**
     * 编辑 WEB 应用
     */
    public CommonResponse<Map<String, Object>> updateWebApp(String jsonBody) {
        String response = CommonUtil.openApiPostRequest(UPDATE_WEB_APP_PATH, jsonBody);
        return JSONUtil.toBean(response, new TypeReference<CommonResponse<Map<String, Object>>>() {}, false);
    }

    private static final String UPDATE_RESOURCE_PATH = "/api/v1/resource/updateResource";

    /**
     * 编辑 隧道应用（与编辑Web应用同一后端接口路径）
     */
    public CommonResponse<Map<String, Object>> updateTunnelApp(String jsonBody) {
        String response = CommonUtil.openApiPostRequest(UPDATE_RESOURCE_PATH, jsonBody);
        return JSONUtil.toBean(response, new TypeReference<CommonResponse<Map<String, Object>>>() {}, false);
    }

}
